/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package extension.game.room;

import com.smartfoxserver.v2.entities.User;
import com.smartfoxserver.v2.entities.data.ISFSObject;
import com.smartfoxserver.v2.entities.data.SFSObject;
import extension.game.room.handler.RoomCommand;
import extension.game.room.handler.RoomFlag;
import extension.game.zone.Command;
import extension.game.zone.Flag;
import java.util.List;
import log.Debug;

/**
 *
 * @author daz
 */
public class GameResponse {
    
    public RoomExtension ext;
    
    public void delay() {
//        try {
//            Thread.sleep(1000);
//        } catch (InterruptedException ex) {
//        }
    }
    
    public GameResponse(RoomExtension ext) {
        this.ext = ext;
    }
    
    public void sendTableInfo(List<String> listNames, List<Long> listMoneys, List<Integer> listStates,
            long betMoney, boolean isPlay, int tableHost, int seatPos, long totalMoney, User user) {
        
        delay();
        ISFSObject resObj = new SFSObject();
        resObj.putUtfStringArray(RoomFlag.LIST_NAMES, listNames);
        resObj.putLongArray(RoomFlag.LIST_MONEY, listMoneys);
        resObj.putIntArray(RoomFlag.LIST_STATES, listStates);
        resObj.putLong(RoomFlag.BET_MONEY, betMoney);
        resObj.putLong("total_money", totalMoney);
        if (isPlay) {
            resObj.putInt(RoomFlag.IS_PLAY, 1);
        } else {
            resObj.putInt(RoomFlag.IS_PLAY, 0);
        }
        resObj.putInt(RoomFlag.SEAT_POS, seatPos);
        resObj.putInt(RoomFlag.TABLE_HOST, tableHost);
        ext.send(RoomCommand.ROOM_INFO, resObj, user);
        log(resObj);
        
    }
    
    public void sendTableState(List<Integer> listEatCard, List<Integer> listFightCard, int playerPos, List<Integer> listPhom, User user) {
        delay();
        ISFSObject resObj = new SFSObject();
        resObj.putIntArray(RoomFlag.EAT_CARD, listEatCard);
        resObj.putIntArray(RoomFlag.FIGHT_CARD, listFightCard);
        resObj.putInt(RoomFlag.PLAYER_POS, playerPos);
        if (listPhom != null && !listPhom.isEmpty()) {
            resObj.putIntArray(RoomFlag.CARD_PHOM, listPhom);
        }
        ext.send(RoomCommand.TABLE_STATE, resObj, user);
        log(resObj);
    }
    
    public void sendUserLeave(int leaveId, int numberPlayers, boolean isContinue, String name) {
        ISFSObject resObj = new SFSObject();
        resObj.putUtfString("player_leave_name", name);
        resObj.putInt(RoomFlag.NUMBER_PLAYERS, numberPlayers);
        resObj.putInt(RoomFlag.LEAVE_ID, leaveId);
        resObj.putInt(RoomFlag.IS_PLAY, isContinue ? 1 : 0);
        ext.send(RoomCommand.PLAYER_LEAVE, resObj, ext.getParentRoom().getUserList());
        log(resObj);
    }
    
    public void sendUpdatePlayers(String name, long money, int seatPos, List<User> listSend) {
        delay();
        ISFSObject resObj = new SFSObject();
        resObj.putUtfString(RoomFlag.PLAYER_NAME, name);
        resObj.putInt(RoomFlag.PLAYER_SEAT, seatPos);
        resObj.putLong(RoomFlag.PLAYER_MONEY, money);
        ext.send(RoomCommand.PLAYER_ENTER_ROOM, resObj, listSend);
        log(resObj);
        
    }
    
    public void sendReadyState(int seat, int curState, int readyResult) {
        delay();
        ISFSObject resObj = new SFSObject();
        resObj.putInt(RoomFlag.READY_RESULT, readyResult);
        resObj.putInt(RoomFlag.PLAYER_SEAT, seat);
        resObj.putInt(RoomFlag.PLAYER_STATE, curState);
        ext.send(RoomCommand.PLAYER_READY, resObj, ext.getParentRoom().getUserList());
        log(resObj);
    }
    
    public void sendReadyState(int seat, int curState, int readyResult, User user) {
        delay();
        ISFSObject resObj = new SFSObject();
        resObj.putInt(RoomFlag.READY_RESULT, readyResult);
        resObj.putInt(RoomFlag.PLAYER_SEAT, seat);
        resObj.putInt(RoomFlag.PLAYER_STATE, curState);
        ext.send(RoomCommand.PLAYER_READY, resObj, user);
        log(resObj);
    }
    
    void sendMoneyChange(long moneyChange, int playerWin, long playerWinMoney, int playerLost, long playerLostMoney) {
        delay();
        ISFSObject resObj = new SFSObject();
        resObj.putLong(RoomFlag.MONEY_CHANGE, moneyChange);
        resObj.putInt(RoomFlag.PLAYER_WIN, playerWin);
        resObj.putLong(RoomFlag.PLAYER_WIN_MONEY, playerWinMoney);
        resObj.putInt(RoomFlag.PLAYER_LOST, playerLost);
        resObj.putLong(RoomFlag.PLAYER_LOST_MONEY, playerLostMoney);
        ext.send(RoomCommand.MONEY_CHANGE, resObj, ext.getParentRoom().getUserList());
        log(resObj);
    }
    
//    void sendPlayerCard(Player phomPlayer) {
//        delay();
//        ISFSObject resObj = new SFSObject();
//        resObj.putIntArray(RoomFlag.PLAYER_CARD, phomPlayer.getListCards());
//        resObj.putInt(RoomFlag.PLAYER_POS, phomPlayer.getSeatPos());
//        ext.send(RoomCommand.PLAYER_CARD, resObj, ext.getParentRoom().getUserList());
//        log(resObj);
//    }
//    
    public void log(ISFSObject resObj) {
        delay();
        if (Debug.isShowLog) {
            Debug.d(resObj.getDump());
        }
    }
    
    public void sendBetMoney(long betMoney, int betResult) {
        delay();
        ISFSObject resObj = new SFSObject();
        resObj.putLong(RoomFlag.BET_MONEY, betMoney);
        resObj.putInt(RoomFlag.BET_RESULT, betResult);
        ext.send(RoomCommand.BET_MONEY, resObj, ext.getParentRoom().getUserList());
        log(resObj);
    }
    
    public void sendTableInfo(String roomName, List<String> listNames, List<Integer> listPos, List<User> user) {
        delay();
        ISFSObject resObj = new SFSObject();
        resObj.putUtfString(Flag.ROOM_NAME, roomName);
        if (listNames != null) {
            resObj.putUtfStringArray(Flag.ROOM_PLAYER_NAME, listNames);
            resObj.putIntArray(Flag.ROOM_PLAYER_POS, listPos);
            ext.send(Command.TABLE_INFO, resObj, user);
        }
        log(resObj);
    }
    
    public void sendUpdateTable(String roomName, int numberPlayer, String groupName, long betMoney, User user) {
        delay();
        ISFSObject resObj = new SFSObject();
        resObj.putUtfString(Flag.ROOM_NAME, roomName);
        resObj.putInt(Flag.NUMBER_PLAYER, numberPlayer);
        resObj.putUtfString(Flag.GROUP_NAME, groupName);
        resObj.putLong(RoomFlag.BET_MONEY, betMoney);
        if (user.isConnected()) {
            ext.send(Command.UPDATE_TABLE, resObj, user);
        }
        log(resObj);
        
    }
    
    public void sendTimeCount(int tableHost) {
        delay();
        ISFSObject resObj = new SFSObject();
        resObj.putInt(RoomFlag.TABLE_HOST, tableHost);
        ext.send(RoomCommand.HOST_COUNT, resObj, ext.getParentRoom().getUserList());
        log(resObj);
    }
    
    public void sendPlayerCount(int seatPos) {
        delay();
        ISFSObject resObj = new SFSObject();
        resObj.putInt("seat_pos_kick", seatPos);
        ext.send(RoomCommand.PLAYER_COUNT, resObj, ext.getParentRoom().getUserList());
        log(resObj);
    }
    
    public void sendPlayerKick(String name, int seatPos, User user) {
        delay();
        ISFSObject resObj = new SFSObject();
        resObj.putUtfString("player_name", name);
        resObj.putInt("player_seat", seatPos);
        ext.send(RoomCommand.KICK, resObj, user);
        log(resObj);
    }
    
    public void sendBetMoney(long betMoney, int betResult, User user) {
        delay();
        ISFSObject resObj = new SFSObject();
        resObj.putLong(RoomFlag.BET_MONEY, betMoney);
        resObj.putInt(RoomFlag.BET_RESULT, betResult);
        ext.send(RoomCommand.BET_MONEY, resObj, user);
        log(resObj);
    }
    
    public void sendDeleteHostTimeOut(int tableHost) {
        delay();
        ISFSObject resObj = new SFSObject();
        resObj.putInt(RoomFlag.TABLE_HOST, tableHost);
        ext.send(RoomCommand.DELETE_HOST_TIME_OUT, resObj, ext.getParentRoom().getUserList());
        log(resObj);
    }
    
    public void sendNotEnough(User user) {
        delay();
        ISFSObject resObj = new SFSObject();
        resObj.putInt("enough_money", 0);
        ext.send(Command.BET_MONEY_INFO, resObj, user);
        log(resObj);
    }
    
    public void sendBetMoneyInfo(long userMoney, long moneyJoinMin, long moneyJoinMax, String roomName, User user) {
        delay();
        ISFSObject resObj = new SFSObject();
        resObj.putInt("enough_money", 1);
        resObj.putLong("user_money", userMoney);
        resObj.putLong("money_min", moneyJoinMin);
        resObj.putLong("money_max", moneyJoinMax);
        resObj.putUtfString("room_name", roomName);
        ext.send(Command.BET_MONEY_INFO, resObj, user);
        log(resObj);
    }
}
